package cn.edu.zafu.bigdata.yt;

import java.util.Random;
import java.util.Scanner;

public class GuessNumberGame {
    private final int numberToGuess;
    private final int maxGuesses;
    private int currentGuesses;

    public GuessNumberGame(int maxGuesses) {
        this.maxGuesses = maxGuesses;
        this.currentGuesses = 0;
        this.numberToGuess = new Random().nextInt(100) + 1; 
    }

    public void startGame() {
        Scanner scanner = new Scanner(System.in);
        boolean hasGuessedCorrectly = false;

        System.out.println("Welcome you have " + maxGuesses + " times to guess.");

        while (currentGuesses < maxGuesses && !hasGuessedCorrectly) {
            System.out.print("guess：");
            int guess = scanner.nextInt();
            currentGuesses++;

            if (guess == numberToGuess) {
                hasGuessedCorrectly = true;
                System.out.println("Confragulation,you are right " + numberToGuess);
            } else if (guess < numberToGuess) {
                System.out.println("Too small,try again.");
            } else if (guess > numberToGuess) {
                System.out.println("Too big,try again.");
            }
        }

        if (!hasGuessedCorrectly) {
            System.out.println("Sry your times run out,the right answer is：" + numberToGuess);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        int maxGuesses = 5; 
        GuessNumberGame game = new GuessNumberGame(maxGuesses);
        game.startGame();

        
        System.out.println("want try again (yes/no)");
        Scanner scanner = new Scanner(System.in);
        String playAgain = scanner.nextLine();
        scanner.close();

        if ("yes".equalsIgnoreCase(playAgain)) {
            game = new GuessNumberGame(maxGuesses);
            game.startGame();
        }
    }
}